package com.yeyuanqi.hottop.controller;


import com.yeyuanqi.hottop.common.Result;
import com.yeyuanqi.hottop.entity.TJinrihot;
import com.yeyuanqi.hottop.service.RedisService;
import com.yeyuanqi.hottop.service.TJinrihotService;
import com.yeyuanqi.hottop.vo.HotListVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yeyuanqi
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/jrhot")
public class TJinrihotController {

    @Autowired
    private TJinrihotService tJinrihotService;
    @Autowired
    private RedisService redisService;

    /**
     * 查询最新的热搜
     * @return 最新热搜列表
     */
    @GetMapping("/newshotlist")
    public Result getNewHot(){
        //查询缓存，有缓存直接返回
        if (redisService.hasKey("jinriNewsHot")){
            Object jinriNewsHot = redisService.get("jinriNewsHot");
            return Result.succ(jinriNewsHot);
        }
        List<HotListVO> hotListVOS = tJinrihotService.selectNewHot();
        return Result.succ(hotListVOS);
    }

    /**
     * 根据日期或者时间查询热搜
     * @return 所查询时间的热搜列表
     */
    @GetMapping("/timehotlist/{day}/{min}")
    public Result selectHotByTime(@PathVariable("day") String day, @PathVariable("min") String min) {
        if ("all".equals(min)) {
            min = "%";
        }else if (!"".equals(min) && null != min){
            min = min + "%";
        }
        List<HotListVO> hotListVOS = tJinrihotService.selectHotByTime(day, min);
        return Result.succ(hotListVOS);
    }

    /**
     * 查询全部热搜
     * @return 全部热搜列表
     */
    @GetMapping("/alllist")
    public Result getAllList(){
        List<TJinrihot> list = tJinrihotService.list();
        return Result.succ(list);
    }
}

